package com.neu.questionnaireplus.controller;

import cn.hutool.crypto.digest.MD5;
import com.neu.questionnaireplus.entity.Admin;
import com.neu.questionnaireplus.entity.HttpResponseEntity;
import com.neu.questionnaireplus.service.AdminService;
import com.neu.questionnaireplus.service.MsmService;
import com.neu.questionnaireplus.utils.constant.HttpStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
@CrossOrigin
@Api("管理员服务")
@Validated
public class AdminController {

  @Autowired
  private AdminService adminService;

  @Autowired
  private MsmService msmService;

  /**
   * @Description: map用来暂时存储短信验证码，key是手机号，value是验证码 ,ConcurrentHashMap保障并发安全
   * @Param:
   * @return:
   * @Author: ~FHX~ (:TSunS:)
   * @Date: 2022/11/2
   */
  private Map<String, String> loginmap = new ConcurrentHashMap<String, String>();
  private Map<String, String> resetPwdmap = new ConcurrentHashMap<String, String>();

  @ApiOperation("管理员登录时发送短信")
  @RequestMapping(value = "/loginSmsAdmin", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity loginSms(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      String phone = map.get("phone").toString();
      String s = msmService.loginSend("86" + phone);
      loginmap.put(phone, s);
      httpResponseEntity
          .setCode(HttpStatus.SUCCESS)
          .setData(s)
          .setMessage("success");
    } catch (Exception e) {
      httpResponseEntity
          .setCode(HttpStatus.ERROR)
          .setData(null)
          .setMessage(e.getMessage());
    } finally {
      return httpResponseEntity;
    }
  }


  @ApiOperation("通过密码登录")
  @RequestMapping(value = "/loginByPwdAdmin", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity loginByPwd(@RequestBody Map<String, Object> map) {
    HttpResponseEntity responseEntity = new HttpResponseEntity();
    try {
      String username = map.get("username").toString();
      String password = map.get("password").toString();
      boolean b = adminService.loginByPassword(username, MD5.create().digestHex(password));
      Admin admin = adminService.queryAdminByUsername(username);
      if (b) {
        responseEntity
            .setCode(HttpStatus.SUCCESS)
            .setData(admin)
            .setMessage("success");
      } else {
        responseEntity
            .setCode(HttpStatus.ERROR)
            .setData(null)
            .setMessage("用户名或密码错误");
      }
    } catch (Exception e) {
      responseEntity
          .setCode(HttpStatus.ERROR)
          .setData(null)
          .setMessage(e.getMessage());
    } finally {
      return responseEntity;
    }
  }


  @ApiOperation("通过短信验证码登录")
  @RequestMapping(value = "/loginBySmsAdmin", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity loginBySms(@RequestBody Map<String, Object> map) {
    HttpResponseEntity responseEntity = new HttpResponseEntity();
    try {
      String phone = map.get("phone").toString();
      String str = map.get("str").toString();
      boolean b = adminService.loginByPhone(phone, str, loginmap.get(phone));
      Admin admin = adminService.queryAdminByPhone(phone);
      if (b) {
        responseEntity
            .setCode(HttpStatus.SUCCESS)
            .setData(admin)
            .setMessage("success");
      } else {
        responseEntity
            .setCode(HttpStatus.ERROR)
            .setData(null)
            .setMessage("手机号或短信验证码错误");
      }
    } catch (Exception e) {
      responseEntity
          .setCode(HttpStatus.ERROR)
          .setData(null)
          .setMessage(e.getMessage());
    } finally {
      return responseEntity;
    }
  }

  @ApiOperation("管理员找回密码时发送短信")
  @RequestMapping(value = "/findPwdSmsAdmin", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity findPwdSms(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      String phone = map.get("phone").toString();
      String s = msmService.resetPwdSend("86" + phone);
      resetPwdmap.put(phone, s);
      httpResponseEntity
          .setCode(HttpStatus.SUCCESS)
          .setData(s)
          .setMessage("success");
    } catch (Exception e) {
      httpResponseEntity
          .setCode(HttpStatus.ERROR)
          .setData(null)
          .setMessage(e.getMessage());
    } finally {
      return httpResponseEntity;
    }
  }


  @ApiOperation("找回密码（重置密码）")
  @RequestMapping(value = "/resetPwdAdmin", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity resetPwd(@RequestBody Map<String, Object> map) {
    HttpResponseEntity responseEntity = new HttpResponseEntity();
    try {
      String username = map.get("username").toString();
      Admin admin = adminService.queryAdminByUsername(username);
      if (admin != null) {
        String newpassword = map.get("newpassword").toString();
        admin.setPassword(MD5.create().digestHex(newpassword));
        boolean b = adminService.updateAdmin(admin);
        if (b) {
          responseEntity
              .setCode(HttpStatus.SUCCESS)
              .setData(admin)
              .setMessage("success");
        } else {
          responseEntity
              .setCode(HttpStatus.ERROR)
              .setData(null)
              .setMessage("修改失败");
        }
      } else {
        responseEntity
            .setCode(HttpStatus.ERROR)
            .setData(null)
            .setMessage("租户不存在");
      }
    } catch (Exception e) {
      responseEntity
          .setCode(HttpStatus.ERROR)
          .setData(null)
          .setMessage(e.getMessage());
    } finally {
      return responseEntity;
    }
  }



}
